package com.anti.modular.bus.busprintsettings.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.anti.core.context.login.LoginContextHolder;
import com.anti.jdbc.DBUtils;
import com.anti.jdbc.support.page.PageParam;
import com.anti.jdbc.support.page.PageResult;
import com.anti.commom.core.exception.BusinessException;
import com.anti.modular.bus.busprintsettings.entity.BusPrintSettings;
import com.anti.modular.bus.busprintsettings.enums.BusPrintSettingsExceptionEnum;
import com.anti.modular.bus.busprintsettings.param.BusPrintSettingsParam;
import com.anti.modular.bus.busprintsettings.result.BusPrintSettingsResult;
import com.anti.modular.bus.busprintsettings.service.BusPrintSettingsService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 打印設置service接口实现类
 *
 * @author anti
 * @date 2021-04-11 09:10:50
 */
@Service
public class BusPrintSettingsServiceImpl implements BusPrintSettingsService {
    @Resource
    DBUtils dbUtils;

    @Override
    public BusPrintSettings getById(BusPrintSettingsParam busPrintSettingsParam){
        String sql="select * from bus_print_settings where id =? ";
        return dbUtils.get(sql,BusPrintSettings.class,busPrintSettingsParam.getId());
    }

    @Override
    public PageResult page(PageParam pageParam,BusPrintSettingsParam busPrintSettingsParam) {
        StringBuilder sb=new StringBuilder("select * from bus_print_settings where 1=1 ");
        ArrayList<Object> param = CollectionUtil.newArrayList();
        if (ObjectUtil.isNotNull(busPrintSettingsParam)) {
            // 根据备注 查询
            if (ObjectUtil.isNotEmpty(busPrintSettingsParam.getRemark())) {
               sb.append(" and remark = ?");
               param.add(busPrintSettingsParam.getRemark());
            }
        }
        return dbUtils.query(pageParam,sb.toString(),param.toArray());
    }

    @Override
    public List<BusPrintSettings> list(BusPrintSettingsParam busPrintSettingsParam) {
        String sql="";
        return dbUtils.query(sql,BusPrintSettings.class);
    }

    @Override
    public void add(BusPrintSettingsParam busPrintSettingsParam) {
        BusPrintSettings busPrintSettings = new BusPrintSettings();
        BeanUtil.copyProperties(busPrintSettingsParam, busPrintSettings);
        busPrintSettings.setCreateTime(new Date());
        busPrintSettings.setCreateUser(LoginContextHolder.me().getSysLoginUserId());
        dbUtils.insert(busPrintSettings);
    }


    public void delete(BusPrintSettingsParam busPrintSettingsParam) {
        String sql="delete  from bus_print_settings where id =? ";
        dbUtils.execSql(sql,busPrintSettingsParam.getId());
    }

    public void edit(BusPrintSettingsParam busPrintSettingsParam) {
        if (ObjectUtil.isEmpty(busPrintSettingsParam.getId())){
            this.add(busPrintSettingsParam);
        }else {
            BusPrintSettings busPrintSettings = this.queryBusPrintSettings(busPrintSettingsParam);
            BeanUtil.copyProperties(busPrintSettingsParam, busPrintSettings);
            busPrintSettings.setUpdateTime(new Date());
            busPrintSettings.setStatus(0);
            busPrintSettings.setUpdateUser(LoginContextHolder.me().getSysLoginUserId());
            dbUtils.update(busPrintSettings);
        }
    }

    @Override
    public BusPrintSettingsResult detail() {
        String sql="select * from bus_print_settings ";
        return dbUtils.getFirst(sql,BusPrintSettingsResult.class);
    }

    /**
     * 获取打印設置
     *
     * @author anti
     * @date 2021-04-11 09:10:50
     */
    private BusPrintSettings queryBusPrintSettings(BusPrintSettingsParam busPrintSettingsParam) {
        BusPrintSettings busPrintSettings = this.getById(busPrintSettingsParam);
        if (ObjectUtil.isNull(busPrintSettings)) {
            throw new BusinessException(BusPrintSettingsExceptionEnum.NOT_EXIST);
        }
        return busPrintSettings;
    }


}
